local ma__shenmou = fk.CreateSkill {
  name = "ma__shenmou",
  tags = { Skill.Switch },
}

Fk:loadTranslationTable{
  ["ma__shenmou"] = "慎谋",
  [":ma__shenmou"] = "转换技，游戏开始时可自选阴阳状态。其他角色的回合开始时，阳：若其体力值不小于你，"..
  "你可以与一名其他角色各摸两张牌并弃置两张牌，本回合你与其不能响应牌且成为牌的目标后使用者随机弃置一张牌；"..
  "阴：若其体力值不大于你，你可以视为对其使用一张【火攻】，若造成伤害，你可以对其上家或下家重复此流程。",

  ["#ma__shenmou_yang-invoke"] = "慎谋：选择任意名角色，这些角色各摸两张牌并弃置两张牌且本回合不能响应牌",
  ["#ma__shenmou_yin-invoke"] = "慎谋：视为使用一张【火攻】，造成伤害后可以对其上下家重复此流程",
  ["#ma__shenmou-choose"] = "慎谋：选择手牌数最多的一名角色，其对 %dest 使用手牌中所有【杀】和伤害锦囊牌",

  ["@@ma__shenmou_yang-turn"] = "慎谋",

  ["$ma__shenmou1"] = "行计以险，纵略以奇，敌虽百万亦戏之如犬豕。",
  ["$ma__shenmou2"] = "若生铸剑为犁之心，须有纵钺止戈之力。",
}

local U = require "packages/utility/utility"

ma__shenmou:addEffect(fk.TurnStart, {
  anim_type = "switch",
  can_trigger = function(self, event, target, player, data)
    if target ~= player and player:hasSkill(ma__shenmou.name) then
      if player:getSwitchSkillState(ma__shenmou.name) == fk.SwitchYang and target.hp >= player.hp then
        return true
      else if target.hp <= player.hp and not player:isNude() then return true end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if player:getSwitchSkillState(ma__shenmou.name) == fk.SwitchYang then
      local tos = room:askToChoosePlayers(player,{
        min_num = 1,
        max_num = 1,
        targets = room:getOtherPlayers(player, false),
        skill_name = ma__shenmou.name,
        prompt = "#ma__shenmou_yang-invoke",
      })
      if #tos > 0 then
        table.insert(tos, player)
        room:sortByAction(tos)
        event:setCostData(self, {tos = tos})
        return true
      end
    else
      local use = room:askToUseVirtualCard(player, {
        name = "fire_attack",
        skill_name = ma__shenmou.name,
        prompt = "#ma__shenmou_yin-invoke",
        cancelable = true,
        extra_data = {
          exclusive_targets = {target.id},
        },
        --[[card_filter = {
          n = 1,
          cards = player:getCardIds("he"),
        }]]
      })
      if use then
      event:setCostData(self, {extra_data = use})
      return true
    end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    U.SetSwitchSkillState(player, ma__shenmou.name, player:getSwitchSkillState(ma__shenmou.name, false))
    if player:getSwitchSkillState(ma__shenmou.name, true) == fk.SwitchYang then
      local tos = event:getCostData(self).tos
      for _, to in ipairs(tos) do
        if not to.dead then
          to:drawCards(2, ma__shenmou.name)
          room:askToDiscard(to, {
          min_num = 2,
          max_num = 2,
          include_equip = true,
          skill_name = ma__shenmou.name,
          cancelable = false,
        })
        room:addPlayerMark(to, "@@ma__shenmou_yang-turn")
        end
      end
    else
      local use = table.simpleClone(event:getCostData(self).extra_data)
      --room:useCard(use)
      while use and use.damageDealt do
        local to = use.tos[1]
        local targets = table.filter(room.alive_players, function (p)
          return p:getNextAlive() == to or to:getNextAlive() == p
        end)
        use = room:askToUseVirtualCard(player, {
        name = "fire_attack",
        skill_name = ma__shenmou.name,
        prompt = "#ma__shenmou_yin-invoke",
        cancelable = true,
        extra_data = {
          exclusive_targets = table.map(targets, Util.IdMapper),
        },
        --[[card_filter = {
          n = 1,
          cards = player:getCardIds("he"),
        }]]
        })
        --[[if use then
          room:useCard(use)
        end]]
      end
    end
  end,
})

ma__shenmou:addEffect(fk.GameStart, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(ma__shenmou.name, true)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = room:askToChoice(player, {
      choices = { "tymou_switch:::ma__shenmou:yang", "tymou_switch:::ma__shenmou:yin" },
      skill_name = ma__shenmou.name,
      prompt = "#tymou_switch-choice:::ma__shenmou",
    })
    choice = choice:endsWith("yang") and fk.SwitchYang or fk.SwitchYin
    U.SetSwitchSkillState(player, ma__shenmou.name, choice)
  end,
})

ma__shenmou:addEffect(fk.CardUsing, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return #table.filter(player.room.alive_players, function(p)
        return p:getMark("@@ma__shenmou_yang-turn") > 0
      end) > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    data.disresponsiveList = data.disresponsiveList or {}
      for _, p in ipairs(room.alive_players) do
        if p:getMark("@@ma__shenmou_yang-turn") > 0 then
          table.insertIfNeed(data.disresponsiveList, p)
        end
      end
  end,
})

ma__shenmou:addEffect(fk.TargetConfirmed, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@@ma__shenmou_yang-turn") > 0 and
      not data.from:isNude()
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(data.from:getCardIds("he"), function (id)
      return not data.from:prohibitDiscard(id)
    end)
    if #cards > 0 then
      room:throwCard(table.random(cards), ma__shenmou.name, data.from, data.from)
    end
  end,
})

return ma__shenmou
